package com.ss.android.article.base.feature.favorite;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.ss.android.action.ArticleActionHelper;
import com.ss.android.article.base.feature.c.FeedAdapter;
import com.ss.android.article.base.feature.c.FeedItemBehavior;
import com.ss.android.article.base.feature.detail.presenter.DetailHelper;
import com.ss.android.article.base.feature.detail2.DetailIntentCreator;
import com.ss.android.article.base.feature.e.ArticleShareHelper;
import com.ss.android.article.base.feature.feed.activity.ArticleListFragment;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryHandler;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryObj;
import com.ss.android.article.base.feature.feed.presenter.ArticleQueryThread;
import com.ss.android.article.base.feature.feed.presenter.ListData;
import com.ss.android.article.base.feature.feed.presenter.QueryCallback;
import com.ss.android.article.base.feature.model.CellRef;
import com.ss.android.article.base.utils.ArticleHelper;
import com.ss.android.article.browser.R;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.model.ItemType;
import com.ss.android.model.BaseArticle;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public final class FavoriteArticleListFragment extends ArticleListFragment implements FeedItemBehavior, QueryCallback {
    protected boolean a;
    private CellRef b;
    private String c;
    private long d;
    private int e;
    private boolean f;
    private long g;
    private boolean h;
    private View mViewContainer;
    private ArticleQueryHandler j;
    private int k;

    public FavoriteArticleListFragment() {
        this.a = true;
        this.f = true;
        this.g = -1;
        this.h = false;
        this.j = new ArticleQueryHandler(this);
    }

    public final void a(boolean arg5) {
        this.mAdapter.onHiddenStatusChanged(arg5);
        this.mAppData.a = System.currentTimeMillis();
    }

    private boolean a() {
        boolean v0 = false;
        if (this.g > 0) {
            long v2 = 9223372036854775807L;
            int v4 = -1;
            int v1;
            for (v1 = 0; v1 < this.mData.size(); ++v1) {
                Object v0_1 = this.mData.get(v1);
                if (v0_1 != null && ((CellRef) v0_1).article != null) {
                    long v6 = Math.abs(this.g - ((CellRef) v0_1).article.userRepinTime);
                    if (v2 > v6) {
                        v2 = v6;
                        v4 = v1;
                    }
                }
            }

            this.mListView.setSelection(this.mListView.getHeaderViewsCount() + v4);
            v0 = true;
        }

        return v0;
    }

    static boolean a(FavoriteArticleListFragment arg1) {
        return arg1.mIsLoading;
    }

    public final void onStartQuery(ArticleQueryObj articleQueryObj) {
    }

    public final void onFinishQuery(boolean arg11, ArticleQueryObj articleQueryObj) {
        if ((this.isViewValid()) && articleQueryObj != null && articleQueryObj.b == this.e) {
            this.mFooter.hideFooterContent();
            this.mIsLoading = false;
            if (!arg11) {
                this.showNotify(this.getString(TTUtils.a(articleQueryObj.errorCode)), true);
                if (this.a) {
                    this.mPullRefreshList.onRefreshComplete();
                    this.a = false;
                }

                this.refreshList();
                if (!this.mData.isEmpty()) {
                    this.a();
                }

                this.g = -1;
            } else {
                if (articleQueryObj.q != null) {
                    Iterator v1 = articleQueryObj.q.iterator();
                    while (v1.hasNext()) {
                        Object v0 = v1.next();
                        if (((CellRef) v0).videoStyle >= 0) {
                            continue;
                        }

                        ((CellRef) v0).videoStyle = 0;
                    }
                }

                List v2 = this.mAppData.a(articleQueryObj.q);
                int v1_1 = 0;
                int v0_1 = 0;
                new ArrayList();
                if (this.a) {
                    this.mListData.h = 0;
                    this.mData.clear();
                    v2 = android.support.design.a.a(this.mData, v2);
                    v1_1 = 1;
                    this.a = false;
                    if (!articleQueryObj.d) {
                        this.mListData.c = articleQueryObj.hasMore;
                    }

                    if (v2.isEmpty()) {
                        this.mListData.d = false;
                    }

                    if (!articleQueryObj.d && (articleQueryObj.j) && (articleQueryObj.v)) {
                        v0_1 = 1;
                    }

                    this.mAdsAppItem = articleQueryObj.mAdsAppItem;
                    if (articleQueryObj.mAdsAppItem != null) {
                        this.showNotify(this.mAdsAppItem, false);
                    }

                    this.mPullRefreshList.onRefreshComplete();
                } else {
                    if (!articleQueryObj.d) {
                        this.mListData.c = articleQueryObj.hasMore;
                    }

                    v2 = android.support.design.a.a(this.mData, v2);
                    if (v2.isEmpty()) {
                        if (!articleQueryObj.d) {
                        } else {
                            this.mListData.d = false;
                        }
                    } else {
                        this.mListData.d = true;
                    }
                }

                if (!v2.isEmpty()) {
                    this.mData.addAll(((Collection) v2));
                }

                if (articleQueryObj.lastCateBehotTime > 0) {
                    if (this.mListData.h > 0 && this.mListData.h <= articleQueryObj.lastCateBehotTime) {
                        this.mListData.h = Math.max(0, this.mListData.h - 1);
                    } else {
                        this.mListData.h = articleQueryObj.lastCateBehotTime;
                    }
                } else {
                    this.mListData.h = Math.max(0, this.mListData.h - 1);
                }

                this.refreshList();
                if (!this.mData.isEmpty() && !this.a() && v1_1 != 0) {
                    this.mListView.setSelection(0);
                }

                this.g = -1;
                if (v0_1 == 0) {
                    return;
                }

                if (!NetworkUtils.isNetworkAvailable(this.mContext)) {
                    return;
                }

                this.a = true;
                this.queryData();
            }
        }
    }

    public final int contextType() {
        return 0;
    }

    public final void doHandleItemClick(int arg7, CellRef arg8, View arg9, boolean arg10, boolean arg11) {
        int v5 = 2;
        if (this.mData != null && !this.mData.isEmpty()) {
            FragmentActivity v1 = this.getActivity();
            if (v1 != null && arg8 != null) {
                this.onEvent("detail");
                this.mListData.b = arg7;
                this.mListData.a = this.mData;
                this.mAppData.a(this.mListData, v5, null);
                this.k = 1;
                this.b = this.mData.get(arg7);
                ArticleHelper.a = true;
                Intent v2 = new Intent();
                v2.putExtra("tag", this.c);
                v2.putExtra("list_type", v5);
                v2.putExtra("view_comments", arg10);
                v2.putExtra("show_write_comment_dialog", arg11);
                this.startActivityForResult(ComponentsManifest.findComponent(DetailIntentCreator.class).createNewDetailIntent(((Context) v1), v2.getExtras()), 110);
            }
        }
    }

    public final String getCategoryName() {
        return "favorite_tab";
    }

    public final String getListCategory() {
        return null;
    }

    public final int getListType() {
        return 2;
    }

    public final boolean getRemoveDislikedItem() {
        return false;
    }

    public final int getViewLayout() {
        return R.layout.d5;
    }

    public final void onActivityCreated(Bundle arg12) {
        super.onActivityCreated(arg12);
        Bundle v0 = this.getArguments();
        if (v0 != null) {
            this.c = v0.getString("tag");
        }

        if (android.support.design.a.isTextEmpty(this.c)) {
            this.c = "news";
        }

        this.mArticleActionHelper = new ArticleActionHelper(this.mContext);
        this.mDetailHelper = new DetailHelper(this.getActivity(), ItemType.ARTICLE, this.j, this.mArticleActionHelper, "xiangping");
        this.mArticleShareHelper = new ArticleShareHelper(this.getActivity(), this.mArticleActionHelper, this.mDetailHelper, 201);
        this.mArticleShareHelper.mCategoryName = "__favor__";
        this.mAdapter = new FeedAdapter(this.getActivity(), this, this.mNetworkMonitor,
                this.mViewContainer, this, 2, this.mDiggAnimationView, this.mArticleActionHelper,
                this.mArticleShareHelper, "__favor__");
        this.registerLifeCycleMonitor(this.mAdapter);
        this.mAdapter.setListView(this.mListView);
        this.mListView.setRecyclerListener(this.mAdapter);
        this.mListView.setAdapter(this.mAdapter);
        this.mPullRefreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

            public final void onPullDownToRefresh(PullToRefreshBase toRefreshBase) {
                if (!FavoriteArticleListFragment.a(FavoriteArticleListFragment.this)) {
                    FavoriteArticleListFragment.this.a = true;
                    FavoriteArticleListFragment.this.queryData();
                }
            }
        });
        this.mIsLoading = false;
        this.h = this.mSpipe.isLogin;
    }

    public final void onActivityResult(int arg7, int arg8, Intent arg9) {
        if (arg7 == 110) {
            if (this.k == 1 && !ArticleHelper.a) {
                this.mData.remove(this.b);
                this.mAdapter.b(this.mData);
            }

            if (arg8 != -1) {
                return;
            }

            ListData v0 = this.mAppData.a(2, null);
            if (v0 == null) {
                return;
            }

            if (this.mAppData.a == this.d) {
                return;
            }

            this.g = v0.i;
        } else {
            super.onActivityResult(arg7, arg8, arg9);
        }
    }

    public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        this.mViewContainer = super.onCreateView(inflater, container, bundle);
        return mViewContainer;
    }

    public final void onDayNightThemeChanged(Resources resources, boolean isNightMode) {
        super.onDayNightThemeChanged(resources, isNightMode);
        if (this.mListView != null) {
            this.mListView.setBackgroundColor(resources.getColor(com.ss.android.article.browser.R.color.activity_bg_color));
        }
    }

    public final void onDeleteFavorClick(CellRef arg2) {
        if (!this.mData.isEmpty()) {
            this.mData.remove(arg2);
            this.refreshList();
        }
    }

    public final void onEvent(String arg3) {
        MobClickCombiner.onEvent(this.getActivity(), "favorite_tab", arg3);
    }

    public final void onEvent(String arg10, BaseArticle arg11) {
        long v6 = 0;
        if (arg11 != null && arg11.groupId > v6) {
            JSONObject v8 = new JSONObject();
            try {
                v8.put("item_id", arg11.itemId);
                v8.put("aggr_type", arg11.aggrType);
            } catch (JSONException v0) {
            }

            MobClickCombiner.onEvent(this.getActivity(), "favorite_tab", arg10, arg11.groupId, v6, v8);
        }
    }

    public final void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            this.mAdapter.onHiddenStatusChanged(false);
            this.mAppData.a = System.currentTimeMillis();
        }
    }

    public final void onResume() {
        super.onResume();
        int v0 = this.refreshList(-1, false);
        long v2 = this.mAppData.a;
        if (!this.mIsLoading && (v2 != this.d || this.h != this.mSpipe.isLogin) && this.k != 1) {
            this.d = v2;
            this.a = true;
            this.h = this.mSpipe.isLogin;
            this.queryData();
        }

        this.k = 0;
        this.b = null;
        ArticleHelper.a = true;
        this.setSelectionFromTop(v0);
    }

    public final void onScroll(AbsListView arg3, int arg4, int arg5, int arg6) {
        super.onScroll(arg3, arg4, arg5, arg6);
        if (this.mAdapter != null) {
            this.mAdapter.a(false);
        }

        if (!this.mIsLoading && !this.mData.isEmpty() && arg6 > 1 && arg6 == arg4 + arg5) {
            this.queryData();
        }
    }

    public final void onViewScrollChanged(int arg3, int arg4, int arg5, int arg6) {
        super.onViewScrollChanged(arg3, arg4, arg5, arg6);
        if (this.mAdapter != null) {
            this.mAdapter.a(false);
        }
    }

    public final void queryData() {
        long v3;
        long v6;
        long v10;
        boolean v2;
        boolean v5;
        long v8 = 0;
        if (this.mData.isEmpty()) {
            this.a = true;
            if (this.f) {
                this.f = false;
                v5 = true;
            } else {
                v5 = false;
            }
        } else {
            v5 = false;
        }

        if (!NetworkUtils.isNetworkAvailable(this.mContext)) {
            v2 = true;
        } else {
            if (!this.a && !this.mListData.c) {
                v2 = true;
            } else {
                v2 = false;
            }
        }

        if ((this.a) || !v2 || (this.mListData.d)) {
            if (!this.a) {
                if (!this.mData.isEmpty()) {
                    v10 = this.mListData.h;
                    Object v0 = this.mData.get(this.mData.size() - 1);
                    if (v0 != null) {
                        if (((CellRef) v0).article != null && ((CellRef) v0).article.userRepinTime > v8) {
                            v6 = ((CellRef) v0).article.userRepinTime;
                        } else {
                            v6 = v8;
                        }
                    } else {
                        v6 = v8;
                    }
                } else {
                    v6 = v8;
                    v10 = v8;
                }

                if (v10 <= v8) {
                    this.mListData.c = false;
                    this.mListData.d = false;
                    return;
                }

                v3 = v10;
            } else {
                v6 = v8;
                v3 = v8;
            }

            ++this.e;
            this.mIsLoading = true;
            this.hideNoDataView();
            if ((this.mData.isEmpty()) || (this.a)) {
                this.mFooter.hideFooterContent();
            } else {
                this.mFooter.switchToLoading();
            }

            if (this.a) {
                this.mPullRefreshList.e();
            }
            new ArticleQueryThread(this.mContext, this.j, new ArticleQueryObj(this.e, v2, v3, v5, v6)).start();
        } else {
            this.mPullRefreshList.onRefreshComplete();
        }
    }

    public final void refreshListHook() {
        if (this.mData.isEmpty()) {
            this.showNoDataView();
        } else {
            this.hideNoDataView();
        }

        FragmentActivity v0 = this.getActivity();
        if (v0 != null && ((v0 instanceof FavoriteActivity)) && !this.isHidden()) {
            boolean v1 = !this.mData.isEmpty() ? true : false;
            ((FavoriteActivity) v0).a(v1);
        }
    }
}

